//
//  RegistViewController.m
//  CNKICommunity
//
//  Created by wei on 17/3/6.
//  Copyright © 2017年 CNKI. All rights reserved.
//

#import "RegistViewController.h"
#import "SubjectSelectViewController.h"
#import "CNKIProfessionalModel.h"
#import "CNKISqlTools.h"
@interface RegistViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITextField *ProfessionalText;
@property(nonatomic,strong)NSArray *ProfessionalArr;
@property(nonatomic,strong)UITableView *SearchTableview;
@end

@implementation RegistViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 100, 100)];
    imageView.image = [UIImage imageNamed:@"tabBar_publish_icon"];
    [self.view addSubview:imageView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(jumpNextPage)];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    [imageView addGestureRecognizer:tap];
    imageView.userInteractionEnabled = YES;
    
    UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(100, 350, 100, 100)];
    imageView1.image = [UIImage imageNamed:@"tabBar_publish_icon"];
    [self.view addSubview:imageView1];
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(DisappearPage)];
    tap1.numberOfTapsRequired = 1;
    tap1.numberOfTouchesRequired = 1;
    [imageView1 addGestureRecognizer:tap1];
    imageView1.userInteractionEnabled = YES;
    
    _ProfessionalText = [[UITextField alloc]initWithFrame:CGRectMake(20, 100, SCREENWIDTH - 40, 30)];
    _ProfessionalText.textColor = [UIColor darkGrayColor];
    _ProfessionalText.font = [UIFont systemFontOfSize:17];
    _ProfessionalText.borderStyle = UITextBorderStyleRoundedRect;
    [_ProfessionalText addTarget:self action:@selector(textfieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:_ProfessionalText];
    
    //创建假数据
    [self addData];
    [self setupTableView];
    
}
-(void)setupTableView
{
    _SearchTableview = [[UITableView alloc]initWithFrame:CGRectMake(20, _ProfessionalText.y + 33, SCREENWIDTH - 40, 150) style:UITableViewStylePlain];
    _SearchTableview.delegate = self;
    _SearchTableview.dataSource = self;
    [self.view addSubview:_SearchTableview];
    _SearchTableview.hidden = YES;
}
-(void)addData
{
    NSArray *names = @[@"西门电工", @"西门厨师", @"西门抽风", @"西门吹雪", @"东门电工", @"东门抽筋", @"东门抽风", @"东门吹雪", @"北门厨师", @"北门抽筋", @"南门抽风", @"南门吹雪"];
        for (int i = 0; i<20; i++) {
                CNKIProfessionalModel *p = [[CNKIProfessionalModel alloc] init];
                p.ProfessionalName = [NSString stringWithFormat:@"%@-%d", names[arc4random_uniform(names.count)], arc4random_uniform(100)];
            [CNKISqlTools save:p];
        }
}
-(void)textfieldDidChanged:(UITextField*)textField
{
    if (_SearchTableview.hidden == YES)
    {
       _SearchTableview.hidden = NO;
    }
    self.ProfessionalArr =  [CNKISqlTools queryWithCondition:textField.text];
    NSLog(@"模糊检索结果%@",self.ProfessionalArr);
    [_SearchTableview reloadData];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.ProfessionalArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Mycell"];
    if (!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Mycell"];
    }
    CNKIProfessionalModel *model = self.ProfessionalArr[indexPath.row];
    cell.textLabel.text = model.ProfessionalName;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CNKIProfessionalModel *model = self.ProfessionalArr[indexPath.row];
    _ProfessionalText.text = model.ProfessionalName;
    _SearchTableview.hidden = YES;
 
}















-(void)jumpNextPage
{
   SubjectSelectViewController *SubjectSelectVC = [[SubjectSelectViewController alloc]init];
   [self presentViewController:SubjectSelectVC animated:YES completion:nil];
}
-(void)DisappearPage
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}

@end
